package pers.wwz.study.tencentcop.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import pers.wwz.study.tencentcop.request.app.AppTextMsgRequest;
import pers.wwz.study.tencentcop.request.app.AppText;

/**
 * 发送应用消息控制器
 */
@RestController
@RequestMapping("/app")
public class SendMessageController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    AccessTokenController accessTokenController;

    @Value("${tencent.agentid}")
    private Integer agentid;

    @Value("${tencent.sendMsgUrlPrefix}")
    private String sendMsgUrlPrefix;

    /**
     * 发送消息
     * 最大长度600
     * @return
     */
    @PostMapping(value = "/sendTextMsg",produces = {"applicatiom/json"})
    public String senMsg(String content) throws Exception {
        AppTextMsgRequest sendMsgRequest = new AppTextMsgRequest();
        // 指定接收消息的成员，成员ID列表（多个接收者用‘|’分隔，最多支持1000个）。特殊情况：指定为”@all”，则向该企业应用的全部成员发送
        sendMsgRequest.setTouser("@all");
        sendMsgRequest.setMsgtype("text");
        sendMsgRequest.setAgentid(agentid);

        AppText text = new AppText();
        text.setContent(content);
        sendMsgRequest.setText(text);
        String result = restTemplate.postForObject(sendMsgUrlPrefix+accessTokenController.getAccessToken(), sendMsgRequest, String.class);
        return result;
    }
}
